//
//  MKUIElementViewSelection.m
//  MKUI
//
//  Created by 苏尚进 on 2023/7/12.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUIElementViewSelection.h"
#import "UIView+MKUI.h"
#import "MKUIPreCompile.h"
#import "NSArray+MKUI.h"

@interface MKUIElementViewSelection(){
    NSMutableArray *_selectedOptions;
}
@end

@implementation MKUIElementViewSelection
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        _selectedOptions = [[NSMutableArray alloc] init];
        self.accessView = self.disclosureIndicatorAccessView;
    }
    return self;
}

- (void)setSelectedOption:(MKUIElementSelectOption *)selectedOption{
    self.selectedOptions = selectedOption?@[selectedOption]:nil;
}
- (MKUIElementSelectOption *)selectedOption{
    return _selectedOptions.firstObject;
}
- (void)setSelectedOptions:(NSArray<MKUIElementSelectOption *> *)selectedOptions{
    [_selectedOptions removeAllObjects];
    if(selectedOptions.count){
        [_selectedOptions addObjectsFromArray:selectedOptions];
    }
    [self __refreshDetailText];
}
- (id)selectedValue{
    return self.selectedOption.value;
}
- (void)setSelectedValue:(id)selectedValue{
    for(MKUIElementSelectOption *option in self.options){
        BOOL eq = [selectedValue conformsToProtocol:@protocol(NSObject)]?option.value==selectedValue||[selectedValue isEqual:option.value]:option.value==selectedValue;
        if(eq){
            self.selectedOption = option;
            return;
        }
    }
}
- (NSArray<MKUIElementSelectOption *> *)selectedOptions{
    if(_selectedOptions.count==0) return nil;
    return [self.options mk_map:^id _Nullable(MKUIElementSelectOption *obj) {
        if([_selectedOptions containsObject:obj]) return obj;
        return nil;
    }];
}
- (NSArray *)selectedValues{
    return [self.selectedOptions mk_map:^id _Nullable(MKUIElementSelectOption *obj) {
        return obj.value;
    }];
}
- (void)setSelectedValues:(NSArray *)selectedValues{
    NSMutableArray *selectedOptions = [[NSMutableArray alloc] init];
    for(MKUIElementSelectOption *option in self.options){
        for(id selectedValue in selectedValues){
            BOOL eq = [selectedValue conformsToProtocol:@protocol(NSObject)]?option.value==selectedValue||[selectedValue isEqual:option.value]:option.value==selectedValue;
            if(eq){
                [selectedOptions addObject:option];
                break;
            }
        }
    }
    self.selectedOptions = selectedOptions;
}
- (NSString *)detailTextWithSelectedOptions{
    return [[self.selectedOptions mk_map:^id _Nullable(MKUIElementSelectOption *obj) {
        return obj.text.length>0?obj.text:[(NSString *)obj.value description];
    }] componentsJoinedByString:@","];
}
- (void)setDetailTextWhenNoSelected:(NSString *)detailTextWhenNoSelected{
    if(_detailTextWhenNoSelected==detailTextWhenNoSelected||[_detailTextWhenNoSelected isEqualToString:detailTextWhenNoSelected]) return;
    _detailTextWhenNoSelected = detailTextWhenNoSelected;
    [self __refreshDetailText];
}
- (void)__refreshDetailText{
    if(_selectedOptions.count>0){
        self.detailText = [self detailTextWithSelectedOptions];
    }else{
        self.detailText = self.detailTextWhenNoSelected;
    }
}
- (void)beginSelectOption{
    MKUIElementSelectOptionViewController *vc = [[MKUIElementSelectOptionViewController alloc] init];
    vc.optionCellClass = self.optionCellClass;
    vc.doneButtonText = self.doneButtonText;
    vc.options = self.options;
    vc.title = self.titleText;
    vc.allowsMultipleSelection = self.allowsMultipleSelection;
    if(self.allowsMultipleSelection){
        vc.selectedOptions = self.selectedOptions;
    }else{
        vc.selectedOption = self.selectedOption;
    }
    vc.whenDone = ^(MKUIElementSelectOptionViewController * _Nonnull vc) {
        [vc.mk_navigationControllerOfFirst popViewControllerAnimated:YES];
        if(self.allowsMultipleSelection){
            self.selectedOptions = vc.selectedOptions;
        }else{
            self.selectedOption = vc.selectedOption;
        }
        if(self.whenDone){
            self.whenDone(self);
        }
    };
    [self.mk_navigationControllerOfFirst pushViewController:vc animated:YES];
}
@end

@implementation MKUIElementListViewCellSelection
+ (Class)elementViewClass{
    return MKUIElementViewSelection.class;
}
- (void)didSelected:(BOOL)selected{
    [super didSelected:selected];
    if(!selected) return;
    [self.elementView beginSelectOption];
}
@end

#import "NSArray+MKUI.h"
@implementation MKUIElementTableViewCellModelSelection
- (NSArray<MKUIElementSelectOption *> *)el_options{
    return self[@"el_options"];
}
- (void)setEl_options:(NSArray<MKUIElementSelectOption *> *)el_options{
    self[@"el_options"] = el_options;
    self.el_elementView.options = el_options;
}
- (BOOL)el_allowsMultipleSelection{
    return [self mk_boolForKeyPath:@"el_allowsMultipleSelection"];
}
- (void)setEl_allowsMultipleSelection:(BOOL)el_allowsMultipleSelection{
    self[@"el_allowsMultipleSelection"] = @(el_allowsMultipleSelection);
    self.el_elementView.allowsMultipleSelection = el_allowsMultipleSelection;
}
- (MKUIElementSelectOption *)el_selectedOption{
    return self[@"el_selectedOption"];
}
- (void)setEl_selectedOption:(MKUIElementSelectOption *)el_selectedOption{
    self[@"el_selectedOption"] = el_selectedOption;
    self.el_elementView.selectedOption = el_selectedOption;
}
- (id)el_selectedValue{
    return self.el_selectedOption.value;
}
- (void)setEl_selectedValue:(id)el_selectedValue{
    for(MKUIElementSelectOption *option in self.el_options){
        if(option.value==el_selectedValue){
            self.el_selectedOption = option;
            return;
        }
    }
}
- (NSArray<MKUIElementSelectOption *> *)el_selectedOptions{
    return self[@"el_selectedOptions"];
}
- (void)setEl_selectedOptions:(NSArray<MKUIElementSelectOption *> *)el_selectedOptions{
    self[@"el_selectedOptions"] = el_selectedOptions;
    self.el_elementView.selectedOptions = el_selectedOptions;
}
- (NSArray *)el_selectedValues{
    return [self.el_selectedOptions mk_map:^id _Nullable(MKUIElementSelectOption *obj) {
        return obj.value;
    }];
}
- (void)setEl_selectedValues:(NSArray *)el_selectedValues{
    NSMutableArray *selectedOptions = [[NSMutableArray alloc] init];
    for(MKUIElementSelectOption *option in self.el_options){
        if([el_selectedValues containsObject:option.value]){
            [selectedOptions addObject:option];
        }
    }
    self.el_selectedOptions = selectedOptions;
}
- (NSString *)el_detailTextWhenNoSelected{
    return self[@"el_detailTextWhenNoSelected"];
}
- (void)setEl_detailTextWhenNoSelected:(NSString *)el_detailTextWhenNoSelected{
    self[@"el_detailTextWhenNoSelected"] = el_detailTextWhenNoSelected;
    self.el_elementView.detailTextWhenNoSelected = el_detailTextWhenNoSelected;
}
- (void (^)(__kindof MKUIElementTableViewCellModelSelection * _Nonnull))el_whenSelectionDone{
    return self[@"el_whenSelectionDone"];
}
- (void)setEl_whenSelectionDone:(void (^)(__kindof MKUIElementTableViewCellModelSelection * _Nonnull))el_whenSelectionDone{
    self[@"el_whenSelectionDone"] = [el_whenSelectionDone copy];
}
- (NSString *)el_doneButtonText{
    return self[@"el_doneButtonText"];
}
- (void)setEl_doneButtonText:(NSString *)el_doneButtonText{
    self[@"el_doneButtonText"] = el_doneButtonText;
    self.el_elementView.doneButtonText = el_doneButtonText;
}
@end

@implementation MKUIElementTableViewCellSelection
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return self;
}
+ (Class)elementViewClass{
    return [MKUIElementViewSelection class];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKUIElementTableViewCellModelSelection *cm = self.cellModel;
    MKUIElementViewSelection *elementView = self.elementView;
    elementView.options = cm.el_options;
    elementView.allowsMultipleSelection = cm.el_allowsMultipleSelection;
    if(elementView.allowsMultipleSelection){
        elementView.selectedOptions = cm.el_selectedOptions;
    }else{
        elementView.selectedOption = cm.el_selectedOption;
    }
    elementView.detailTextWhenNoSelected = cm.el_detailTextWhenNoSelected;
    @MK_WEAKIFY(self);
    elementView.whenDone = ^(__kindof MKUIElementViewSelection * _Nonnull el) {
        @MK_NORMALIZE(self);
        MKUIElementTableViewCellModelSelection *cm = self.cellModel;
        if(el.allowsMultipleSelection){
            cm.el_selectedOptions = el.selectedOptions;
        }else{
            cm.el_selectedOption = el.selectedOption;
        }
        if(cm.el_whenSelectionDone){
            cm.el_whenSelectionDone(self.cellModel);
        }
    };
    elementView.doneButtonText = cm.el_doneButtonText;
}
- (void)tableView:(UITableView *)tableView didSelectCell:(BOOL)selected{
    if(!selected) return;
    [self beginSelectOption];
}
- (void)beginSelectOption{
    [self.elementView beginSelectOption];
}
@end
